﻿using System;

namespace FirstFloor.Documents.Pdf.Functions
{
    /// <summary>
    /// Represents a PDF Type 2 Exponential Interpolation function.
    /// </summary>
    internal class ExponentialInterpolationFunction
        : Function
    {
        private double[] c0 = new double[] { 0 };
        private double[] c1 = new double[] { 1 };
        private double n;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExponentialInterpolationFunction"/> class.
        /// </summary>
        /// <param name="dictionary">The function dictionary.</param>
        public ExponentialInterpolationFunction(DictionaryObject dictionary)
            : base(dictionary)
        {
            ArrayObject c0;
            if (dictionary.TryGet<ArrayObject>("C0", out c0)) {
                this.c0 = c0.ToArray<double>();
            }
            ArrayObject c1;
            if (dictionary.TryGet<ArrayObject>("C1", out c1)) {
                this.c1 = c1.ToArray<double>();
            }
            this.n = dictionary.Get<double>("N");
        }

        /// <summary>
        /// Provides the actual implementation of the function.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        protected override double[] OnExecute(params double[] args)
        {
            var x = args[0];
            var y = new double[this.N];
            for (var j = 0; j < this.N; j++) {
                y[j] = this.c0[j] + Math.Pow(x, this.n) * (this.c1[j] - this.c0[j]);
            }
            return y;
        }
    }
}
